package android.mobile.peakgames.net.aspectjandroid.aspect;

import android.util.Log;
import org.aspectj.lang.ProceedingJoinPoint;

//@Aspect
public class ProfilingAspect {
  private static final String TAG = ProfilingAspect.class.getName();
  private static final long MAX_ELAPSED_TIME = 1000;

  //    @Pointcut("execution(String android.mobile.peakgames.net.aspectjandroid.AspectActivity.doHttpCall(..))")
  public void doHttpCallEntryPoint() {
  }

  //    @Pointcut("execution(* android.mobile.peakgames.net.aspectjandroid.AspectActivity.fetchImage(..))")
  public void loadImageEntryPoint() {
  }

  //    @Around("doHttpCallEntryPoint() || loadImageEntryPoint()")
  public Object doHttpCallMethod(ProceedingJoinPoint joinPoint) throws Throwable {
    Object returnValue = null;

    long beginTime = System.currentTimeMillis();

    returnValue = joinPoint.proceed();

    long endTime = System.currentTimeMillis();
    long elapsedTime = (endTime - beginTime);

    Log.d(TAG, joinPoint.getSignature().getName() + " elapsed " + elapsedTime + " ms");
    if (MAX_ELAPSED_TIME < elapsedTime) {
      Log.e(TAG, joinPoint.getSignature()
          + " exceeded MAX_ELAPSED_TIME, the process is taking too much time");
    }

    return returnValue;
  }
}
